<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/9/10 19:12
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Caigou extends WX_Controller
{
    /**
     * 初始化
     * @throws Exception
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 上传
     */
    public function upload()
    {
        $mime = ['image/png', 'image/jpeg', 'image/gif', 'image/jpg'];
        do {
            $file = value($_FILES, 'file');
            if (empty($file)) {
                $this->outputError("请上传正确的图片类型");
                break;
            }

            if (!in_array($file['type'], $mime)) {
                $this->outputError("上传的图片格式不正确");
                break;
            }

            if ($file['size'] > 5 * 1024 * 1024) {
                $this->outputError("请上传小于5M的图片");
                break;
            }
            $res = local($file);
            if (empty($res)) {
                $this->outputError("上传失败");
            } else {
                $send = array_merge($res, array(
                    'thumb' => thumb($res['key'], 140, 140),
                    'source' => thumb($res['key']),
                ));
                $this->output($send);
            }
        } while(false);

    }

    /**
     * 基地详情
     */
    public function apply()
    {
        if (!empty($this->openId)) {
            $send['state'] = false;
            do {
                $this->load->model('common/Caigou_Model', 'c');
                $info = $this->c->checkIfExists($this->userid);
                if (!empty($info)) {
                    if ($info['status'] == 0) {
                        $send['error'] = '申请正在审核中';
                        break;
                    }

                    if ($info['status'] == 6) {
                        $send['error'] = '您已是采购员用户';
                        break;
                    }
                }

                $this->c->apply($this->userid, $this->input->post());

            } while(false);

            $this->output($send);

        } else {
            $this->outputError(self::NO_LOGIN);
        }
    }

}